package common

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
)

type SiteAnnouncementRepository struct {
}

func NewSiteAnnouncementRepository() SiteAnnouncementRepositoryIface {
	return &SiteAnnouncementRepository{}
}

func (r *SiteAnnouncementRepository) SaveSiteAnnouncement(ctx context.Context, request *pb.SaveSiteAnnouncementRequest) (*pb.SaveSiteAnnouncementResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteAnnouncementServiceClient(conn)
	return client.SaveSiteAnnouncement(ctx, request)
}

func (r *SiteAnnouncementRepository) GetSiteAnnouncementManageList(ctx context.Context, request *pb.GetSiteAnnouncementListRequest) (*pb.GetSiteAnnouncementListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteAnnouncementServiceClient(conn)
	return client.GetSiteAnnouncementList(ctx, request)
}

func (r *SiteAnnouncementRepository) GetSiteAnnouncementDetail(ctx context.Context, request *pb.GetSiteAnnouncementDetailRequest) (*pb.GetSiteAnnouncementDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteAnnouncementServiceClient(conn)
	return client.GetSiteAnnouncementDetail(ctx, request)
}

func (r *SiteAnnouncementRepository) DeleteSiteAnnouncement(ctx context.Context, request *pb.DeleteSiteAnnouncementRequest) (*pb.DeleteSiteAnnouncementResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteAnnouncementServiceClient(conn)
	return client.DeleteSiteAnnouncement(ctx, request)
}

func (r *SiteAnnouncementRepository) GetUserSiteAnnouncementList(ctx context.Context, request *pb.GetUserSiteAnnouncementListRequest) (*pb.GetUserSiteAnnouncementListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteAnnouncementServiceClient(conn)
	return client.GetUserSiteAnnouncementList(ctx, request)
}

func (r *SiteAnnouncementRepository) SetUserSiteAnnouncementRead(ctx context.Context, request *pb.SetSiteAnnouncementReadRequest) (*pb.SetSiteAnnouncementReadResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewSiteAnnouncementServiceClient(conn)
	return client.SetSiteAnnouncementRead(ctx, request)
}
